﻿Imports System.ComponentModel
Imports Microsoft.Win32

Public Class ClsWebbutil
    Public Shared Sub SetWebBrowserFeatures(ieVersion As Integer)
	' don't change the registry if running in-proc inside Visual Studio  
	If LicenseManager.UsageMode <> LicenseUsageMode.Runtime Then
		Return
	End If
	'获取程序及名称  
	Dim appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
	'得到浏览器的模式的值  
	Dim ieMode As UInt32 = GeoEmulationModee(ieVersion)
	Dim featureControlRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\"
	'设置浏览器对应用程序（appName）以什么模式（ieMode）运行  
	Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION", appName, ieMode, RegistryValueKind.DWord)
	' enable the features which are "On" for the full Internet Explorer browser  
	'不晓得设置有什么用  
	Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", appName, 1, RegistryValueKind.DWord)


End Sub
''' <summary>  
''' 获取浏览器的版本  
''' </summary>  
''' <returns></returns>  
Public Shared Function GetBrowserVersion() As Integer
	Dim browserVersion As Integer = 0
	Using ieKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Internet Explorer", RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.QueryValues)
		Dim version = ieKey.GetValue("svcVersion")
		If version Is Nothing Then
			version = ieKey.GetValue("Version")
			If version Is Nothing Then
				Throw New ApplicationException("Microsoft Internet Explorer is required!")
			End If
		End If
		Integer.TryParse(version.ToString().Split("."C)(0), browserVersion)
	End Using
	'如果小于7  
	If browserVersion < 7 Then
		Throw New ApplicationException("不支持的浏览器版本!")
	End If
	Return browserVersion
End Function
''' <summary>  
''' 通过版本得到浏览器模式的值  
''' </summary>  
''' <param name="browserVersion"></param>  
''' <returns></returns>  
Public Shared Function GeoEmulationModee(browserVersion As Integer) As UInt32
	Dim mode As UInt32 = 11000
	' Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.   
	Select Case browserVersion
		Case 7
			mode = 7000
			' Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.   
			Exit Select
		Case 8
			mode = 8000
			' Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.   
			Exit Select
		Case 9
			mode = 9000
			' Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.                      
			Exit Select
		Case 10
			mode = 10000
			' Internet Explorer 10.  
			Exit Select
		Case 11
			mode = 11000
			' Internet Explorer 11  
			Exit Select
	End Select
	Return mode
End Function

End Class